package com.zeei.ems.base.security.respone;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 封装统一的返回值
 */
@Data
@NoArgsConstructor
public class CommonResult {
    public static final boolean SUCCESS = true;
    public static final int SUCCESS_CODE = 10000;

    public static final boolean FAIL = false;
    public static final int FAIL_CODE = 11111;
    //操作是否成功
    boolean success = SUCCESS;
    private Integer code = SUCCESS_CODE;
    private String message;

    public CommonResult(ResultCode resultCode) {
        this.success = resultCode.success();
        this.code = resultCode.code();
        this.message = resultCode.message();
    }

    public CommonResult(boolean success, Integer code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public static CommonResult createResult(boolean flag, String message) {
        if (flag) {
            return new CommonResult(flag, CommonCode.FAIL.code(), message);
        } else {
            return new CommonResult(flag, CommonCode.SUCCESS.code(), message);
        }
    }

    public static CommonResult FAIL() {
        return new CommonResult(CommonCode.FAIL);
    }


    /**
     * 没有登录
     */
    public static CommonResult UNAUTHENTICATED() {
        return new CommonResult(CommonCode.UNAUTHENTICATED);
    }

    /**
     * 无权限
     */
    public static CommonResult UNAUTHORISE() {
        return new CommonResult(CommonCode.UNAUTHORISE);
    }

    public static CommonResult FAIL(String message) {
        return new CommonResult(CommonCode.FAIL.success(), CommonCode.FAIL.code(), message);
    }

    public static CommonResult SUCCESS() {
        return new CommonResult(CommonCode.SUCCESS);
    }

    public static CommonResult SUCCESS(String message) {
        return new CommonResult(CommonCode.SUCCESS.success(), CommonCode.SUCCESS.code(), message);
    }

}
